package com.clz.mycustomview.anim

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationSet
import android.view.animation.TranslateAnimation
import android.widget.Toast
import com.clz.mycustomview.R
import kotlinx.android.synthetic.main.activity_object_animator.*
import kotlin.math.cos
import kotlin.math.sin

/**
 * 属性动画的使用
 */
class ObjectAnimatorActivity : AppCompatActivity(), View.OnClickListener {
    var valueAnimator: ValueAnimator? = null
    var objectAnimator: ObjectAnimator? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_object_animator)
        btn_start_anim.setOnClickListener(this)
        btn_cancel_anim.setOnClickListener(this)
        tv_anim.setOnClickListener(this)
        menu.setOnClickListener(this)
        item1.setOnClickListener(this)
        item2.setOnClickListener(this)
        item3.setOnClickListener(this)
        item4.setOnClickListener(this)
        item5.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.menu, R.id.item1, R.id.item2, R.id.item3, R.id.item4, R.id.item5 -> {
                menuOpenAnimation(v)
            }
            R.id.btn_start_anim -> {
                objectAnimatorAnimationSet()
            }
            R.id.btn_cancel_anim -> {
                valueAnimator?.cancel()
            }
            R.id.tv_anim -> {
                Toast.makeText(this, "clicked me", Toast.LENGTH_SHORT).show()
            }
        }
    }

    private var isMenuOpen = false
    private fun menuOpenAnimation(view: View) {
        if (!isMenuOpen) {
            openMenu()
            isMenuOpen = true
        } else {
            isMenuOpen = false
            if (view.id != R.id.menu) {
                Toast.makeText(this, "你点击了" + view.id, Toast.LENGTH_SHORT).show()
            }
            closeMenu()
        }
    }

    private fun openMenu() {
        doAnimationOpen(item1, 0, 5, 300);
        doAnimationOpen(item2, 1, 5, 300);
        doAnimationOpen(item3, 2, 5, 300);
        doAnimationOpen(item4, 3, 5, 300);
        doAnimationOpen(item5, 4, 5, 300);
    }

    private fun closeMenu() {
        doAnimateClose(item1, 0, 5, 300);
        doAnimateClose(item2, 1, 5, 300);
        doAnimateClose(item3, 2, 5, 300);
        doAnimateClose(item4, 3, 5, 300);
        doAnimateClose(item5, 4, 5, 300);
    }

    /**
     * @param view 执行动画的View
     * @param index view在动画序列中的顺序，从0开始
     * @param total  动画序列的个数
     * @param radius 动画半径
     *               <p/>
     *               Math.sin(x):x -- 为number类型的弧度，角度乘以0.017(2π/360)可以转变为弧度
     */

    private fun doAnimationOpen(view: View, index: Int, total: Int, radius: Int) {
        if (view.visibility != View.VISIBLE) {
            view.visibility = View.VISIBLE
        }
        val degree = Math.toRadians(90.0) / (total - 1) * index
        val translationX = -(radius * sin(degree)).toFloat()
        val translationY = -(radius * cos(degree)).toFloat()
        val animatorSet = AnimatorSet()
        // 包含平移，缩放，和透明度动画
        animatorSet.playTogether(
                ObjectAnimator.ofFloat(view, "translationX", 0f, translationX),
                ObjectAnimator.ofFloat(view, "translationY", 0f, translationY),
                ObjectAnimator.ofFloat(view, "scaleX", 0f, 1f),
                ObjectAnimator.ofFloat(view, "scaleY", 0f, 1f),
                ObjectAnimator.ofFloat(view, "alpha", 0f, 1f))
        animatorSet.duration = 500
        animatorSet.start()
    }

    /**
     * @param view 执行动画的View
     * @param index view在动画序列中的顺序，从0开始
     * @param total  动画序列的个数
     * @param radius 动画半径
     *               <p/>
     *               Math.sin(x):x -- 为number类型的弧度，角度乘以0.017(2π/360)可以转变为弧度
     */

    private fun doAnimateClose(view: View, index: Int, total: Int, radius: Int) {
        if (view.getVisibility() != View.VISIBLE) {
            view.setVisibility(View.VISIBLE);
        }
        val degree = Math.toRadians(90.0) / (total - 1) * index
        val translationX = -(radius * sin(degree)).toFloat()
        val translationY = -(radius * cos(degree)).toFloat()
        val animatorSet = AnimatorSet()
        // 包含平移，缩放，和透明度动画
        animatorSet.playTogether(
                ObjectAnimator.ofFloat(view, "translationX", translationX, 0f),
                ObjectAnimator.ofFloat(view, "translationY", translationY, 0f),
                ObjectAnimator.ofFloat(view, "scaleX", 1f, 0.1f),
                ObjectAnimator.ofFloat(view, "scaleY", 1f, 0.1f),
                ObjectAnimator.ofFloat(view, "alpha", 1f, 0f));
        animatorSet.duration = 500
        animatorSet.start()
    }

    private fun objectAnimatorAnimationSet() {
        val tv1BgAnimator = ObjectAnimator.ofInt(tv1, "BackgroundColor", 0xffff00ff.toInt(), 0xffffff00.toInt(), 0xffff00ff.toInt())

        val tv1TranslateY = ObjectAnimator.ofFloat(tv1, "translationY", 0f, 300f, 0f)
        val tv2TranslateY = ObjectAnimator.ofFloat(tv2, "translationY", 0f, 400f, 0f)
        val animationSet = AnimatorSet()
        // 顺序依次播放
        // animationSet.playSequentially(tv1BgAnimator, tv1TranslateY, tv2TranslateY)
        // 动画同时进行
        // animationSet.playTogether(tv1BgAnimator, tv1TranslateY, tv2TranslateY)
        val builder = animationSet.play(tv1TranslateY);
        builder.with(tv2TranslateY).after(tv1BgAnimator);
        animationSet.duration = 1000
        animationSet.start()
    }

    private fun objectAnimator() {
        // objectAnimator = ObjectAnimator.ofFloat(tv_anim,"rotation",0f,360f)
        //-objectAnimator = ObjectAnimator.ofFloat(tv_anim,"translationX",0f,200f,-200f,0f)
        objectAnimator = ObjectAnimator.ofFloat(tv_anim, "scaleX", 0f, 3f, 1f)
        // objectAnimator?.repeatCount = ObjectAnimator.INFINITE
        // objectAnimator?.repeatMode = ObjectAnimator.RESTART
        objectAnimator?.duration = 3000
        objectAnimator?.start()
    }

    private fun valueAnimatorRepeat() {
        valueAnimator = ValueAnimator.ofInt(0, 400)
        valueAnimator?.duration = 1000
        // 监听动画过程中值的实时变化
        valueAnimator?.addUpdateListener {
            val curValue: Int = it?.animatedValue as Int
            tv_anim.layout(tv_anim.left, curValue + btn_start_anim.height, tv_anim.right, curValue + tv_anim.height)
        }
        // 监听动画的播放，暂停，结束，重复
        valueAnimator?.addListener(object : Animator.AnimatorListener {
            override fun onAnimationRepeat(animation: Animator?) {
                TODO("Not yet implemented")
            }

            override fun onAnimationEnd(animation: Animator?) {
                TODO("Not yet implemented")
            }

            override fun onAnimationCancel(animation: Animator?) {
                TODO("Not yet implemented")
            }

            override fun onAnimationStart(animation: Animator?) {
                TODO("Not yet implemented")
            }

        })
        // 设置为无限循环
        valueAnimator?.repeatCount = ValueAnimator.INFINITE
        // 逆序开始循环
        valueAnimator?.repeatMode = ValueAnimator.REVERSE
        valueAnimator?.start();
    }

    private fun valueAnimator() {
        val valueAnimator = ValueAnimator.ofInt(0, 400)
        valueAnimator.duration = 1000
        // 监听动画过程中值的实时变化
        valueAnimator.addUpdateListener {
            val curValue: Int = it?.animatedValue as Int
            tv_anim.layout(curValue, curValue, curValue + tv_anim.width, curValue + tv_anim.height)
        }
        valueAnimator.start()
    }

    fun viewAnimation() {
        val translateAnimation = TranslateAnimation(Animation.ABSOLUTE, 0f,
                Animation.ABSOLUTE, 400f, Animation.ABSOLUTE, 0f, Animation.ABSOLUTE, 100f)
        translateAnimation.fillAfter = true
        translateAnimation.duration = 1000
        tv_anim.startAnimation(translateAnimation)
    }

    override fun onDestroy() {
        super.onDestroy()
        valueAnimator?.cancel()
    }
}